package com.example.test_20210705_serialization;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.*;

public class Person implements Serializable,Comparator,Comparable{

    private String name;
    private Integer age;
    private String addr;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", addr='" + addr + '\'' +
                '}';
    }

    public Person(){

    }
    public Person(String name,Integer age,String addr){
        this.name = name;
        this.addr = addr;
        this.age = age;
    }

    @Override
    public int compare(Object p1, Object p2) {
        Person o1 = (Person) p1;
        Person o2 = (Person) p2;
        if(o1.age > o2.age){
            return 1;
        }else if(o1.age.equals(o2.age)){
            return 0;
        }else{
            return -1;
        }
    }
    @Override
    public int compareTo(Object o) {
        Person o1 = (Person) o;
        if(this.age > o1.age){
            return 1;
        }
        return 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return name.equals(person.name) &&
                age.equals(person.age);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

//   static class compor implements Comparator{
//        @Override
//        public int compare(Object p1, Object p2) {
//            Person o1 = (Person) p1;
//            Person o2 = (Person) p2;
//            if(o1.age > o2.age){
//                return 1;
//            }else if(o1.age.equals(o2.age)){
//                return 0;
//            }else{
//                return -1;
//            }
//        }
//    }

    public static void main(String[] args) {
        Person person1 = new Person("张三",28,"雅逸花园小区1");
        Person person2 = new Person("张三",29,"雅逸花园小区2");
        Person person3 = new Person("张三",12,"雅逸花园小区3");
        HashSet<Person> hashSet = new HashSet<>();
        hashSet.add(person1);
        hashSet.add(person2);
        hashSet.add(person3);
        System.out.println(hashSet);
        System.out.println("------------------------");
        TreeSet<Person> treeSet1 = new TreeSet<>();
        TreeSet<Person> treeSet = new TreeSet<>((o1,o2)->{
            if(o1.age > o2.age){
                return 1;
            }else if(o1.age.equals(o2.age)){
                return 0;
            }else{
                return -1;
            }
        });
        treeSet1.add(person1);
        treeSet1.add(person2);
        treeSet1.add(person3);
        System.out.println(treeSet1);
        System.out.println(person3.toString());
        System.out.println(person3.getName());

        System.out.println("------------------------------------");

        String s = JSON.toJSONString(person1);
        System.out.println("s="+s);
        Map map = JSONObject.parseObject(s, Map.class);
        Map map1 = JSON.parseObject(s, Map.class);
        System.out.println("map111111="+map1);
        System.out.println("map1111111111111111="+map);
        Object addr = map.get("addr");
        System.out.println("addrMap="+addr);
        JSONObject jsonObject = JSON.parseObject(s);
        jsonObject.put("aaa","bbb");
        System.out.println(jsonObject.get("aaa"));
        System.out.println(jsonObject.get("addr"));
        System.out.println("--------------------JSONObject-List------------------------------");
        List<Person> list1 = new ArrayList<>();
        list1.add(person1);
        list1.add(person2);
        list1.add(person3);
        String list2 = JSON.toJSONString(list1);
        System.out.println("----------------map----------------------------");
        List<Person> people = JSON.parseArray(list2, Person.class);
        System.out.println("people="+people);
        JSONArray objects = JSON.parseArray(list2);
        for (Object a1:objects) {
            JSONObject a11 = (JSONObject) a1;
//            Person person = JSONObject.toJavaObject(a11, Person.class);
            Person person = JSON.toJavaObject(a11, Person.class);
            System.out.println("result="+person.getAddr());
        }

        System.out.println("---------------JSONObject---------------------");
        JSONObject ob = (JSONObject)JSONObject.toJSON(person3);
        System.out.println("ob111111111111111111111111111111="+ob);
        ob.put("addr","我们的家");
        Person person = JSONObject.toJavaObject(ob, Person.class);
        System.out.println("person="+person);
        System.out.println("--------------JSON----------------------");
        JSONObject o = (JSONObject)JSON.toJSON(person2);
        JSON JSON1 = (JSON)JSON.toJSON(person2);
        System.out.println(o.get("addr"));
        Person person4 = JSON.toJavaObject( o, Person.class);
        Person person5 = JSON.toJavaObject( JSON1, Person.class);
        System.out.println("person4="+person4);
        System.out.println("person5="+person5);
        System.out.println("--------------ObjectInputStream----------------------");
        try(OutputStream outputStream = new FileOutputStream("D:\\PuyuanWork\\WorkGit_NW\\demo\\src\\main\\java\\com\\example\\test_20210705_serialization/output2.txt");
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)){
            List<Person> list = new ArrayList<>();
            list.add(person1);
            list.add(person2);
            list.add(person3);
            objectOutputStream.writeObject(list);
        }catch (Exception e){

        }

        try(InputStream fileInputStream = new FileInputStream("D:\\PuyuanWork\\WorkGit_NW\\demo\\src\\main\\java\\com\\example\\test_20210705_serialization/output2.txt");
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)){
            System.out.println("o111111111111111111111111111111111111");
            List<Person> o1 = (List<Person>)objectInputStream.readObject();
            for (Person p:o1) {
                System.out.println("p="+p);
            }
        }catch (Exception e){

        }



    }

}
